package com.cloud.job.v1;

import cn.hutool.json.JSONUtil;
import com.cloud.constant.RedisKeyConstants;
import com.cloud.pojo.movie.MovieAttr;
import com.cloud.util.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @author: TianXiang
 * @description: 缓存视频播放路径JOB, 以便随机播放使用
 * @date: 2020/11/30
 */

//@Component
public class CacheMovieFilePathJob {

    private static final Logger LOG = LoggerFactory.getLogger(CacheMovieFilePathJob.class.getName());

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 每天凌晨同步
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        redisTemplate.delete(RedisKeyConstants.MOVIE_LIST_KEY);
        redisTemplate.delete(RedisKeyConstants.GREEN_MOVIE_LIST_KEY);
        LOG.info("定时任务执行: 视频路径同步到缓存");
        List<MovieAttr> movieAttrList = new ArrayList<>(), greenMovieAttrList = new ArrayList<>();
        String moviePath, greenMoviePath;
        if(SysUtils.isLinux()) {
            moviePath = "/usr/local/app/nginx/html/movies";
            greenMoviePath = "/usr/local/app/nginx/html/greens";
        }else {
            moviePath = "C:\\Develop\\movies";
            greenMoviePath = "C:\\Develop\\greens";
        }
        for (File dateDir : Arrays.asList(new File(moviePath).listFiles())) {
            if(dateDir.isDirectory()) {
                List<File> dirList = Arrays.asList(dateDir.listFiles());
                for (File dir : dirList) {
                    if(dateDir.isDirectory()) {
                        List<String> fileNameList = Arrays.stream(dir.listFiles())
                                .filter(item -> -1 != item.getName().indexOf("m3u8"))
                                .map(item -> item.getName())
                                .collect(Collectors.toList());
                        fileNameList.forEach(item -> {
                            movieAttrList.add(new MovieAttr().setDateDirName(dateDir.getName()).setDirName(dir.getName()).setMovieName(item));
                        });
                    }
                }
            }
        }

        for (File dateDir : Arrays.asList(new File(greenMoviePath).listFiles())) {
            if(dateDir.isDirectory()) {
                List<File> dirList = Arrays.asList(dateDir.listFiles());
                for (File dir : dirList) {
                    if(dateDir.isDirectory()) {
                        List<String> fileNameList = Arrays.stream(dir.listFiles())
                                .filter(item -> -1 != item.getName().indexOf("m3u8"))
                                .map(item -> item.getName())
                                .collect(Collectors.toList());
                        fileNameList.forEach(item -> {
                            greenMovieAttrList.add(new MovieAttr().setDateDirName(dateDir.getName()).setDirName(dir.getName()).setMovieName(item));
                        });
                    }
                }
            }
        }
        LOG.info("movieAttrList : {}  greenMovieAttrList: {}", movieAttrList,greenMovieAttrList);
        // 记入到redis-list数据结构
        ListOperations<String, String> redisList = redisTemplate.opsForList();
        List<String> listJson = new ArrayList<>();
        for (MovieAttr movieAttr : movieAttrList) {
            listJson.add(JSONUtil.toJsonStr(movieAttr));
        }
        redisList.rightPushAll(RedisKeyConstants.MOVIE_LIST_KEY,listJson);
        listJson.clear();
        for (MovieAttr movieAttr : greenMovieAttrList) {
            listJson.add(JSONUtil.toJsonStr(movieAttr));
        }
        redisList.rightPushAll(RedisKeyConstants.GREEN_MOVIE_LIST_KEY,listJson);
        LOG.info("定时任务完成");
    }

}
